package com.talkweb.securitycamera.data.bean;

import com.talkweb.securitycamera.common.constans.FileType;
import com.talkweb.securitycamera.common.interfaces.entity.IBaseFileEntity;
import com.talkweb.securitycamera.utils.MD5Utils;
import ohos.agp.utils.TextTool;
import ohos.utils.zson.ZSONObject;

import java.io.File;
import java.io.Serializable;

import static com.talkweb.securitycamera.data.DbConstant.ColumeName;

/**
 * @version 1.0.1
 * @description:图像/视频数据bean
 * @program: security-camera
 * @Author xiaozhijun
 * @Date 2021/1/28 9:02
 */
public class MediaEntity implements Serializable, IBaseFileEntity {

    /**
     * 文件路径
     */
    String filePath;

    /**
     * 时长（视频类独有）
     */
    String createDuration;

    /**
     * 文件名
     */
    String fileName;

    /**
     * 文件类型
     */
    FileType fileType;

    /**
     * 创建时间
     */
    long createTime;

    @Override
    public String getFilePath() {
        return filePath;
    }

    @Override
    public String getCreateDuration() {
        return createDuration;
    }

    @Override
    public String getFileName() {
        return fileName != null ? fileName : "";
    }

    @Override
    public FileType getFileType() {
        return fileType;
    }

    public long getCreateTime() {
        return createTime;
    }

    public void setCreateTime(long createTime) {
        this.createTime = createTime;
    }

    public void setFilePath(String filepath) {
        this.filePath = filepath;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public void setType(FileType type) {
        this.fileType = type;
    }

    public void setCreateDuration(String createDuration) {
        this.createDuration = createDuration;
    }

    public void delete() {
        File file = new File(getFilePath());
        file.delete();
    }

    public String toJson() {
        ZSONObject zsonObject = new ZSONObject();
        if (!TextTool.isNullOrEmpty(filePath)) {
            zsonObject.put(ColumeName.COLUME_FILEPATH, getFilePath());
        }
        zsonObject.put(ColumeName.COLUME_FILENAME, getFileName());
        zsonObject.put(ColumeName.COLUME_FILETYPE, getFileType().getValue());
        if (!TextTool.isNullOrEmpty(createDuration)) {
            zsonObject.put(ColumeName.COLUME_DURTION, getCreateDuration());
        }
        zsonObject.put(ColumeName.COLUME_CREATETIME, getCreateTime());
        return zsonObject.toString();
    }

    public String getKeyMd5() {
        return "path_" + MD5Utils.encryptMD5ToString(getFilePath());
    }

}
